package gavdi.dk.controller;

import gavdi.dk.data.DAOCustomer;
import gavdi.dk.model.AbsenceRegistration;
import gavdi.dk.model.Customer;
import gavdi.dk.model.TimeRegistration;
import gavdi.dk.util.Tools;
import gavdi.dk.view.R;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import junit.framework.TestListener;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;





public class WBSAdapter extends ArrayAdapter<Customer>{

    Context context; 
    int layoutResourceId;    
    static List<Customer> Customer;
    protected static List<Customer> testList;
    

    private static class CustomerHolder {
    	int position;
        TextView txtTitle;
        TextView txtTitle2;
    }
    

    
    public WBSAdapter(Context context, int layoutResourceId, List<Customer> Customer) {
        super(context, layoutResourceId, Customer);
        System.out.println("test1");
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        WBSAdapter.Customer = Customer;


    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	System.out.println("getView " + position + " " + convertView);

        CustomerHolder holder;
        
    
 
        if(convertView == null)
        {
        	
		//	LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        	LayoutInflater inflater = LayoutInflater.from(context); 
        	convertView = inflater.inflate(layoutResourceId, null);

            holder = new CustomerHolder();
            holder.txtTitle = (TextView)convertView.findViewById(R.id.txtTitle);
            holder.txtTitle2 = (TextView)convertView.findViewById(R.id.txtTitle2);

            
            holder.position = position;

            


  
            convertView.setTag(holder);
        }
        else {
        	holder = (CustomerHolder) convertView.getTag();
        }
		
		

      
        new CustomerTask(position, holder)
        .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);



     
        	return convertView;
        	
        	
    }
    
    private static class CustomerTask extends AsyncTask {
        private int ePosition;
        private CustomerHolder eHolder;
        

        public CustomerTask(int position, CustomerHolder holder) {
            ePosition = position;
            eHolder = holder;
        }

   

		@Override
		protected Object doInBackground(Object... params) {
		
	

			
			return null;
		}
		
		@Override
		protected void onPostExecute(Object result) {
		      // if (eHolder.position == ePosition) {

				Customer c = Customer.get(ePosition);
			
				eHolder.txtTitle.setText(c.getName());
				eHolder.txtTitle2.setText(c.getWbs());

		    	
		       //}
		       
	//	 

		}





    }

    
    

  
    
}